﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ASPNETX;
using System.Threading;

namespace SE_Demo
{
    public partial class IndexerForm : Form
    {
        private Indexer _indexer;
        private Thread t1;
        
        public IndexerForm()
        {
            InitializeComponent();

            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;

            _indexer = new Indexer();
        }

        private void btnIndex_Click(object sender, EventArgs e)
        {
            btnIndex.Enabled = false;
            t1 = new Thread(new ThreadStart(BackGroundProcess));
            t1.Start();
            //btnIndex.Enabled = true;
        }

        private void BackGroundProcess()
        {
            btnIndex.Text = "Going...";

            ASPNETX.Seeker mySeeker = new ASPNETX.Seeker();
            mySeeker.RootURL = tbURL.Text;

            lbURL.Items.Clear();

            lbURL.Items.Insert(0, "索引开始于:"+DateTime.Now.ToString());

            _indexer.AddRecord(tbURL.Text);
            lbURL.Items.Insert(0, "主页检索完毕");

            List<string> urlList = mySeeker.GetURLs(1);

            lbURL.Items.Insert(0, "检索到其子页面:" + urlList.Count.ToString()+"个");

            int i = 1;
            
            foreach (string url in urlList)
            {
                lbURL.Items.Insert(0,"("+i.ToString()+"/"+urlList.Count.ToString()+")-"+url);
                _indexer.AddRecord(url);
                i++;
            }

            _indexer.Index();

            lbURL.Items.Insert(0, "索引完成于:" + DateTime.Now.ToString());

            btnIndex.Enabled = true;
            btnIndex.Text = "Index again";
        }
    }
}
